{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "517b9b91",
   "metadata": {},
   "source": [
    "#### 列表扩展中括号记法"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "20ccae88",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "saying = \"Don't panic!\"\n",
    "letters = list(saying)\n",
    "letters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "8f75810d",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D'"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "92e1c5ac",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"'\""
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "69fca0a3",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'p'"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "633e1691",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'!'"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "cdc87ed6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'i'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[-3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2d16db9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'p'"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[-6]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "3266cdae",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'D'"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "first = letters[0]\n",
    "last = letters[-1]\n",
    "first"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "43cc00b8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'!'"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "last"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "114bb5d4",
   "metadata": {},
   "source": [
    "#### 列表切片的使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "3c477632",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f8b80c06",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['D', \"'\", 'p', 'i']"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[0:10:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "3cc235c2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[\"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[3:]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "a2a2d421",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i']"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[:10]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "cdf10ae2",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['D', 'n', 't', 'p', 'n', 'c']"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "letters[::2]"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7cd1f837",
   "metadata": {},
   "source": [
    "#### 开始和结束列表"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "b7fd01d1",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['T',\n",
       " 'h',\n",
       " 'e',\n",
       " ' ',\n",
       " 'H',\n",
       " 'i',\n",
       " 't',\n",
       " 'c',\n",
       " 'h',\n",
       " 'h',\n",
       " 'i',\n",
       " 'k',\n",
       " 'e',\n",
       " 'r',\n",
       " \"'\",\n",
       " 's',\n",
       " ' ',\n",
       " 'G',\n",
       " 'u',\n",
       " 'i',\n",
       " 'd',\n",
       " 'e',\n",
       " ' ',\n",
       " 't',\n",
       " 'o',\n",
       " ' ',\n",
       " 't',\n",
       " 'h',\n",
       " 'e',\n",
       " ' ',\n",
       " 'G',\n",
       " 'a',\n",
       " 'l',\n",
       " 'a',\n",
       " 'x',\n",
       " 'y']"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "book = \"The Hitchhiker's Guide to the Galaxy\"\n",
    "booklist = list(book)\n",
    "booklist"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "3e9d346f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['T', 'h', 'e']"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "booklist[0:3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "93527240",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'The'"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "''.join(booklist[0:3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "1e6235fa",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Galaxy'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "''.join(booklist[-6:])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "85a1ee08",
   "metadata": {},
   "source": [
    "#### 列表中使用步长"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "0a47506c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"yxalaG eht ot ediuG s'rekihhctiH ehT\""
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "backwards = booklist[::-1]\n",
    "''.join(backwards)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "id": "ce600846",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\"TeHthie' ud oteGlx\""
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "every_other = booklist[::2]\n",
    "''.join(every_other)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "id": "a883f28c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'Hitchhiker'"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "''.join(booklist[4:14])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "19b2a0df",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'rekihhctiH'"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "''.join(booklist[13:3:-1])"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "34b4de12",
   "metadata": {},
   "source": [
    "#### 在列表中使用切片\n",
    "#### 把“Don't panic!”转换成“on tap”"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "b4a7004c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['o', 'n', ' ', 't', 'a', 'p']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "for i in range(4):\n",
    "    plist.pop()\n",
    "plist.pop(0)\n",
    "plist.remove(\"'\")\n",
    "plist.extend([plist.pop(),plist.pop()])\n",
    "plist.insert(2,plist.pop(3))\n",
    "new_phrase = ''.join(plist)\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "a7763110",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Don't panic!\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "['D', 'o', 'n', \"'\", 't', ' ', 'p', 'a', 'n', 'i', 'c', '!']\n",
      "on tap\n"
     ]
    }
   ],
   "source": [
    "phrase = \"Don't panic!\"\n",
    "plist = list(phrase)\n",
    "print(phrase)\n",
    "print(plist)\n",
    "\n",
    "new_phrase = ''.join(plist[1:3])\n",
    "new_phrase = new_phrase + ''.join([plist[5],plist[4],plist[7],plist[6]])\n",
    "\n",
    "print(plist)\n",
    "print(new_phrase)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "6dce11ef",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
